package com.up.vms.infrastructure.mvc;

import com.up.vms.infrastructure.enums.BizeTypeCode;
import com.up.vms.infrastructure.enums.GeneralRespCode;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

/**
 * @author Andy
 * @since 2017-05-24 16:34
 */
public class ReturnCode extends AbstractReturnCode {
	
	private static final long serialVersionUID = 8495964481648488130L;

    private final String code;
    
    private final String message;
    
    private String detailMessage;

    public static final ReturnCode SUCCESS = new ReturnCode(GeneralRespCode.SUCCESS);
    public static final ReturnCode SERVICE_FAILED = new ReturnCode(GeneralRespCode.SERVICE_FAILED);
    public static final ReturnCode BAD_PARAMETERS = new ReturnCode(GeneralRespCode.BAD_PARAMETERS);
    public static final ReturnCode MISS_PARAMETERS = new ReturnCode(GeneralRespCode.MISS_PARAMETERS);
    public static final ReturnCode DATA_DECRYPT_FAILED = new ReturnCode(GeneralRespCode.DATA_DECRYPT_FAILED);
    public static final ReturnCode FORMAT_PARSE_FAILED = new ReturnCode(GeneralRespCode.FORMAT_PARSE_FAILED);
    public static final ReturnCode API_NOT_FOUND = new ReturnCode(GeneralRespCode.API_NOT_FOUND);
    public static final ReturnCode REQUEST_UNAUTHORIZED = new ReturnCode(GeneralRespCode.REQUEST_UNAUTHORIZED);
    public static final ReturnCode ACCESS_OVER_FREQUENCY = new ReturnCode(GeneralRespCode.ACCESS_OVER_FREQUENCY);
    public static final ReturnCode UNSUPPORT_CHANNEL = new ReturnCode(GeneralRespCode.UNSUPPORT_CHANNEL);

    public static final ReturnCode RANDOM_CODE_CREATE_FAILED = new ReturnCode(GeneralRespCode.RANDOM_CODE_CREATE_FAILED);
    public static final ReturnCode RANDOM_CODE_SEND_FAILED = new ReturnCode(GeneralRespCode.RANDOM_CODE_SEND_FAILED);
    public static final ReturnCode VERIFICATION_CODE_NOT_EXIST = new ReturnCode(GeneralRespCode.VERIFICATION_CODE_NOT_EXIST);
    public static final ReturnCode VERIFICATION_CODE_INVALID = new ReturnCode(GeneralRespCode.VERIFICATION_CODE_INVALID);
    public static final ReturnCode VERIFY_CODE_FAILED = new ReturnCode(GeneralRespCode.VERIFY_CODE_FAILED);

    public static final ReturnCode SMS_SEND_OVER_TIMES = new ReturnCode(GeneralRespCode.SMS_SEND_OVER_TIMES);

    public static final ReturnCode SERVICE_ERROR = new ReturnCode(GeneralRespCode.SERVICE_ERROR);

    public static final ReturnCode REMOTE_AUTH_FAILED = new ReturnCode(GeneralRespCode.REMOTE_AUTH_FAILED);
    public static final ReturnCode REMOTE_SERVICE_FAILED = new ReturnCode(GeneralRespCode.REMOTE_SERVICE_FAILED);
    public static final ReturnCode REMOTE_SERVICE_INFO_ERROR = new ReturnCode(GeneralRespCode.REMOTE_SERVICE_INFO_ERROR);

    public static final ReturnCode VERIFY_PAY_PASSWORD_FAILED = new ReturnCode(GeneralRespCode.VERIFY_PAY_PASSWORD_FAILED);

    public static final ReturnCode SYSTEM_LOGIN_NEED = new ReturnCode(GeneralRespCode.SYSTEM_LOGIN_NEED);
    public static final ReturnCode SYSTEM_LOGIN_FAILED = new ReturnCode(GeneralRespCode.SYSTEM_LOGIN_FAILED);
    public static final ReturnCode SYSTEM_LOGIN_TIMEOUT = new ReturnCode(GeneralRespCode.SYSTEM_LOGIN_TIMEOUT);
    public static final ReturnCode SYSTEM_LOGIN_NULL = new ReturnCode(GeneralRespCode.SYSTEM_LOGIN_NULL);

    /**
     * 业务状态码定义如下
     *
     */
    public static final ReturnCode USER_ALREADY_EXIST = new ReturnCode((BizeTypeCode.USER_ALREADY_EXIST));
    public static final ReturnCode USER_NOT_EXIST = new ReturnCode((BizeTypeCode.USER_NOT_EXIST));
    public static final ReturnCode COUPON_ALREADY_VERIFIED = new ReturnCode(BizeTypeCode.COUPON_ALREADY_VERIFIED);
    public static final ReturnCode COUPON_ALREADY_RECEIVED = new ReturnCode(BizeTypeCode.COUPON_ALREADY_RECEIVED);
    public static final ReturnCode COUPON_NOT_EXIST = new ReturnCode(BizeTypeCode.COUPON_NOT_EXIST);
    public static final ReturnCode NOT_ALLOW_TO_RECEIVE = new ReturnCode(BizeTypeCode.NOT_ALLOW_TO_RECEIVE);
    public static final ReturnCode CODE_ALREADY_EXCHANGED = new ReturnCode(BizeTypeCode.CODE_ALREADY_EXCHANGED);
    public static final ReturnCode INVALID_EXCHANGE_CODE = new ReturnCode(BizeTypeCode.INVALID_EXCHANGE_CODE);
    public static final ReturnCode NO_PUBLISHED_COUPON = new ReturnCode(BizeTypeCode.NO_PUBLISHED_COUPON);

    public static final ReturnCode MERCHANT_USER_IS_NOT_EXIST = new ReturnCode(BizeTypeCode.MERCHANT_USER_IS_NOT_EXIST);
    public static final ReturnCode MERCHANT_USER_PWD_IS_NOT_VALID = new ReturnCode(BizeTypeCode.MERCHANT_USER_PWD_IS_NOT_VALID);
    public static final ReturnCode MERCHANT_NOT_ALLOW_TO_VERIFY = new ReturnCode(BizeTypeCode.MERCHANT_NOT_ALLOW_TO_VERIFY);
    public static final ReturnCode MERCHANT_NOT_EXIST = new ReturnCode(BizeTypeCode.MERCHANT_NOT_EXIST);
    public static final ReturnCode MERCHANT_ITEM_IS_NOT_EXIST = new ReturnCode(BizeTypeCode.MERCHANT_ITEM_IS_NOT_EXIST);
    public static final ReturnCode MERCHANT_USER_IS_EXIST = new ReturnCode(BizeTypeCode.MERCHANT_USER_IS_EXIST);
    public static final ReturnCode MERCHANT_IS_INVALID_VERIFY_CODE = new ReturnCode(BizeTypeCode.MERCHANT_IS_INVALID_VERIFY_CODE);

    public static final ReturnCode ACTIVITY_VOUCHER_ALREADY_VERIFIED = new ReturnCode(BizeTypeCode.ACTIVITY_VOUCHER_ALREADY_VERIFIED);
    public static final ReturnCode ACTIVITY_VOUCHER_ALREADY_EXPIRED = new ReturnCode(BizeTypeCode.ACTIVITY_VOUCHER_ALREADY_EXPIRED);
    public static final ReturnCode ACTIVITY_VOUCHER_NOT_ALLOW_TO_VERIFY = new ReturnCode(BizeTypeCode.ACTIVITY_VOUCHER_NOT_ALLOW_TO_VERIFY);

    public static final ReturnCode ORDER_HAVE_UNPAID_ORDERS = new ReturnCode(BizeTypeCode.ORDER_HAVE_UNPAID_ORDERS);
    public static final ReturnCode ORDER_HAVE_REGISTRATION = new ReturnCode(BizeTypeCode.ORDER_HAVE_REGISTRATION);

    public static final ReturnCode NO_SIGN_OPEN = new ReturnCode(BizeTypeCode.NO_SIGN_OPEN);
    public static final ReturnCode SIGN_NOT_OPEN = new ReturnCode(BizeTypeCode.SIGN_NOT_OPEN);
    public static final ReturnCode SIGN_NOT_START = new ReturnCode(BizeTypeCode.SIGN_NOT_START);
    public static final ReturnCode SIGN_IS_END = new ReturnCode(BizeTypeCode.SIGN_IS_END);
    public static final ReturnCode SIGN_SYNC_FAILED = new ReturnCode(BizeTypeCode.SIGN_SYNC_FAILED);
    public static final ReturnCode USER_IS_SIGNED = new ReturnCode(BizeTypeCode.USER_IS_SIGNED);

    public static final ReturnCode INVALID_INVITATION_CODE = new ReturnCode(BizeTypeCode.INVALID_INVITATION_CODE);
    public static final ReturnCode GIFT_CARD_CODE_USED = new ReturnCode(BizeTypeCode.GIFT_CARD_CODE_USED);
    public static final ReturnCode MEMBER_REGISTERED = new ReturnCode(BizeTypeCode.MEMBER_REGISTERED);

    /**
     * 这个通过static final 统一处理, 不需要public
     *
     * @param respCode
     */
    private ReturnCode(GeneralRespCode respCode) {
        this.code = respCode.code;
        this.message = respCode.desc;
    }

    private ReturnCode(BizeTypeCode bizRespCode) {
        this.code = bizRespCode.code;
        this.message = bizRespCode.desc;
    }

    public ReturnCode(String code, String message, String detailMessage) {
        this(code, message);
        this.detailMessage = StringUtils.trimToEmpty(detailMessage);
    }

    public ReturnCode(String code, String message) {
        this.code = (StringUtils.isEmpty(code) ? SUCCESS.code : code);
        this.message = StringUtils.trimToEmpty(message);
    }

    public final ReturnCode appendMessage(String detailMessage) {
        //returncode都是static变量，必须要new一个新的
        return new ReturnCode(this.code, this.message, detailMessage);
    }

    public final boolean isSuccess() {
        return SUCCESS.code.equals(this.code);
    }

    public final boolean isFail() {
        return !isSuccess();
    }

    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

	public String getDetailMessage() {
		return detailMessage;
	}

	public void setDetailMessage(String detailMessage) {
		this.detailMessage = detailMessage;
	}

	public String getCode() {
		return code;
	}

	public String getMessage() {
		return message;
	}
    
}
